Cloud SQL is a fully-managed database service that makes it easy to set up, maintain, manage, and administer your relational databases on Google Cloud Platform.
!pip install pymysql \
import pymysql
The Cloud SQL Proxy provides secure access to your Cloud SQL Second Generation instances without having to whitelist IP addresses or configure SSL.
In [ ]:
!wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
!chmod +x cloud_sql_proxy
Invoke Cloud SQL proxy without using any authentication
In [ ]:
!./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 &
Invoke Cloud SQL proxy using any Service account JSON
In [ ]:
!./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 \
-credential_file=<PATH_TO_KEY_FILE> &
In [ ]:
import pymysql
# TODO(developer):
# Change USERNAME and PASSWORD to the user and password created on Cloud SQL instance
# Set DB to the name of the database to be connected to
connection = pymysql.connect(host='127.0.0.1',
user='USERNAME',
password='PASSWORD',
db='DB')
Create a cursor for this connection to interact with the database.
In [ ]:
mycursor = connection.cursor()
In [ ]:
mycursor.execute("create table EMPLOYEE ( \
EMP_ID bigint not null, \
EMP_NAME varchar(50) not null, \
EMP_NO varchar(20) not null, \
HIRE_DATE date not null, \
IMAGE longblob, \
JOB varchar(30) not null, \
SALARY float not null, \
DEPT_ID integer not null, \
MNG_ID bigint, \
primary key (EMP_ID), \
unique (EMP_NO) \
);")
mycursor.fetchall()
print(mycursor.description)
In [ ]:
mycursor.execute("insert into EMPLOYEE (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) \
values (7839, 'KING', 'E7839', Str_To_Date('17-11-1981', '%d-%m-%Y'), 'PRESIDENT', 5000, 10, null);")
In [ ]:
mycursor.execute("SELECT * FROM EMPLOYEE")
mycursor.fetchall()
In [ ]:
#Execute a SQL command
mycursor.execute(SQL_COMMAND)
# Display all the rows from output of the previous execution using fetchall()
mycursor.fetchall()
# Display only one row from output of the previous execution using fetchall()
mycursor.fetchone()